package com.atguigu.srb.mybatisPlus;

import com.atguigu.srb.mybatisPlus.entity.Product;
import com.atguigu.srb.mybatisPlus.mapper.ProductMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

//乐观锁的测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class test3 {
    @Autowired
    private ProductMapper productMapper;
    @Test
    public void testConcurrentUpdate(){
        //1；小李
        Product p1 = productMapper.selectById(1L);
        //2；小王
        Product p2 = productMapper.selectById(1L);
        //3;小李将价格加50.存入数据
        p1.setPrice(p1.getPrice()+50);
        int result1 = productMapper.updateById(p1);
        System.out.println("效力修改的结果是= " + result1);
        //4；小王将商品减了30，存入数据库
        p2.setPrice(p2.getPrice()-30);
        int result2 = productMapper.updateById(p2);
        if (result2==0){
            System.out.println("小王重试" );
            //重新获取数据
            p2=productMapper.selectById(1L);
            p2.setPrice(p2.getPrice()-30);
            productMapper.updateById(p2);
        }
        System.out.println("小王修改的结果是 = " + result2);
        //最后的结果是
        Product p3 = productMapper.selectById(1L);
        System.out.println("最后的结果 = " + p3.getPrice());
    }
}
